package utils.utilcode.blankj.com.myapplication;

import android.animation.Animator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;

/**
 * Created by Administrator on 2017-12-04.
 */

public class MyView extends View {
    private Context context;
    private int mWidth;
    private Paint mPaintText;
    private Paint mPaintBg;
    private Rect mBound;
    private int startPosition;
    private String mMTitleText;
    private int mHeight;
    private GestureDetector gestureDetector = new GestureDetector(context, new MyOnGestureListener());
    private boolean isLong = true;


    public MyView(Context context) {
        this(context, null);
    }

    public MyView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public MyView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context) {

        this.context = context;
        mPaintText = new Paint();
        mPaintText.setColor(Color.WHITE);
        //抗锯齿
        mPaintText.setAntiAlias(true);
        mPaintText.setTextSize(DisplayUtil.sp2px(context, 18));
        mPaintText.setTextAlign(Paint.Align.CENTER);
        mBound = new Rect();
//先设置大小才有效
        mMTitleText = "钟红文";
        mPaintText.getTextBounds(mMTitleText, 0, mMTitleText.length(), mBound);//你可以得到能够包裹文字的最小矩形
//        文本宽高mBounds.width             用以下面画Text时便宜x轴，y轴


        mPaintBg = new Paint();
        mPaintBg.setStrokeWidth(7);
//
//        //画add
//        mPaintAdd = new Paint();
//        mPaintAdd.setStyle(Paint.Style.FILL);
//        mPaintAdd.setColor(Color.WHITE);
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (isLong) {
            mPaintBg.setAntiAlias(true);
            mPaintBg.setStyle(Paint.Style.FILL);
            mPaintBg.setColor(Color.BLACK);
//        mWidth, mHeight控件的宽高
            //绘制左边的轮子
            canvas.drawArc(new RectF(startPosition, 0, mHeight + startPosition, mHeight), 90, 180, false, mPaintBg);
            //绘制中间的矩形
            canvas.drawRect(new RectF(mHeight / 2 + startPosition, 0, mWidth - mHeight / 2, mHeight), mPaintBg);
            //右边的轮子
            canvas.drawArc(new RectF(mWidth - mHeight, 0, mWidth, mHeight), 180, 270, false, mPaintBg);
            //绘制文字
            canvas.drawText(mMTitleText, getMeasuredWidth() / 2, getMeasuredHeight() / 2 + mBound.height() / 2, mPaintText);
        } else {
            drawAdd(canvas);
            drawMinus(canvas);

        }

    }

    private void drawMinus(Canvas canvas) {
        mPaintBg.setStyle(Paint.Style.STROKE);
        mPaintBg.setStrokeWidth(7);
        canvas.drawCircle(mHeight/2,mHeight/2 , mHeight / 2-7, mPaintBg);
        mPaintText.setStrokeWidth(10);
        mPaintText.setColor(Color.RED);
        canvas.drawLine(mHeight/4, mHeight/2,3*mHeight/4,  mHeight/2, mPaintText);
    }

    private void drawAdd(Canvas canvas) {

        canvas.drawCircle(mWidth - mHeight/2,mHeight/2 , mHeight / 2, mPaintBg);
//绘制直线  ，左上右下
        mPaintText.setStrokeWidth(10);
        canvas.drawLine(mWidth-3*mHeight/4, mHeight/2, mWidth-mHeight/4,  mHeight/2, mPaintText);
        canvas.drawLine(mWidth-mHeight/2, mHeight/4,mWidth-mHeight/2,  3*mHeight/4, mPaintText);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        /**
         * 设置宽度
         */
        int specMode = MeasureSpec.getMode(widthMeasureSpec);
        int specSize = MeasureSpec.getSize(widthMeasureSpec);
        //在该模式下，View的测量大小即为SpecSize。
        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
        {

            mWidth = specSize;
        } else {
//            // 由图片决定的宽
//            int desireByImg = getPaddingLeft() + getPaddingRight() + mImage.getWidth();
            // 由字体决定的宽
            int desireByTitle = getPaddingLeft() + getPaddingRight() + mBound.width();
            //在该模式下，View的测量大小不能超过SpecSize。
            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
//                int desire = Math.max(desireByImg, desireByTitle);
                mWidth = Math.min(desireByTitle, specSize);

            }
        }
/***
 * 设置高度
 */

        specMode = MeasureSpec.getMode(heightMeasureSpec);
        specSize = MeasureSpec.getSize(heightMeasureSpec);
        if (specMode == MeasureSpec.EXACTLY)// match_parent , accurate
        {
            mHeight = specSize;
        } else {
            int desire = getPaddingTop() + getPaddingBottom() + mBound.height();
            if (specMode == MeasureSpec.AT_MOST)// wrap_content
            {
                mHeight = Math.min(desire, specSize);
            }
        }
        setMeasuredDimension(mWidth, mHeight);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        gestureDetector.onTouchEvent(event);
        return true;
    }

    class MyOnGestureListener extends GestureDetector.SimpleOnGestureListener {
        @Override
        public boolean onSingleTapConfirmed(MotionEvent e) {
            if(isLong)
            startAnim(isLong);
            return super.onSingleTapConfirmed(e);
        }

    }

    private void startAnim(boolean a) {
        ValueAnimator valueAnimator = null;
        if (a) {
            valueAnimator = ValueAnimator.ofInt(0, mWidth - mHeight);
        } else {
            valueAnimator = ValueAnimator.ofInt(mWidth - mHeight, 0);
        }

        valueAnimator.setDuration(200);
        // 匀速插值器
        valueAnimator.setInterpolator(new LinearInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                //得到距离
                startPosition = (int) animation.getAnimatedValue();
                invalidate();
//                // 获取当前点坐标封装到mCurrentPosition
//                mPathMeasure.getPosTan(value, mCurrentPosition, null);
//                goods.setTranslationX(mCurrentPosition[0]);//  goods.setX(mCurrentPosition[0]);
//                goods.setTranslationY(mCurrentPosition[1]);

            }
        });
        valueAnimator.start();

//动画监听，动画执行完了移除goods
        valueAnimator.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animator) {

            }

            @Override
            public void onAnimationEnd(Animator animator) {
                isLong = !isLong;

            }

            @Override
            public void onAnimationCancel(Animator animator) {

            }

            @Override
            public void onAnimationRepeat(Animator animator) {

            }

        });
    }
}
